package com.guigu.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.enums.pickuptask.PickupDispatchTaskAssignedStatus;
import com.guigu.common.enums.pickuptask.PickupDispatchTaskStatus;
import com.guigu.work.dto.TaskPickupDispatchDTO;
import com.guigu.work.entity.TaskPickupDispatch;
import com.guigu.work.mapper.TaskPickupDispatchMapper;
import com.guigu.work.service.TaskPickupDispatchService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 取件、派件任务信息表 服务实现类
 * </p>
 *
 * @author liAo
 * @since 2021-11-09
 */
@Service
@Transactional
public class TaskPickupDispatchServiceImpl extends ServiceImpl<TaskPickupDispatchMapper, TaskPickupDispatch> implements TaskPickupDispatchService {

    @Autowired(required = false)
    private TaskPickupDispatchMapper mapper;

    /**
     * 新增取派件任务
     *
     * @param taskPickupDispatch
     * @return TaskPickupDispatch
     */
    @Override
    public TaskPickupDispatch saveTaskPickupDispatch(TaskPickupDispatch taskPickupDispatch) {
        if (null == taskPickupDispatch) {
            throw new IllegalArgumentException("参数异常!");
        }
        //status 1为待执行（对应 待上门和须交接）、2为进行中（该状态暂不使用，属于保留状态）、3为待确认
        taskPickupDispatch.setStatus(PickupDispatchTaskStatus.PENDING.getCode());
        //create_time
        taskPickupDispatch.setCreateTime(new Date());
        //assigned_status 1未分配2已分配3待人工分配
       // taskPickupDispatch.setAssignedStatus(PickupDispatchTaskAssignedStatus.TO_BE_DISTRIBUTED.getCode());
        this.saveOrUpdate(taskPickupDispatch);
        return taskPickupDispatch;
    }

    /**
     * 修改取派件任务信息
     *
     * @param taskPickupDispatchDTO
     * @return TaskPickupDispatch
     */
    @Override
    public TaskPickupDispatch updateMsgById(TaskPickupDispatchDTO taskPickupDispatchDTO) {
        if (null == taskPickupDispatchDTO) {
            throw new IllegalArgumentException("参数异常!");
        }

        //派送员不为空,修改任务分配状态
        if (StringUtils.isNotEmpty(taskPickupDispatchDTO.getCourierId())) {
            taskPickupDispatchDTO.setAssignedStatus(PickupDispatchTaskAssignedStatus.DISTRIBUTED.getCode());
        }
        this.updateById(taskPickupDispatchDTO);
        return taskPickupDispatchDTO;
    }

    /**
     * 分页 条件查询运单
     *
     * @param dto
     * @return
     */
    @Override
    public IPage<TaskPickupDispatch> findByPage(TaskPickupDispatchDTO dto) {
        if (null == dto) {
            throw new IllegalArgumentException("参数异常!");
        }
        Page<TaskPickupDispatch> IPage =
                new Page<TaskPickupDispatch>(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<TaskPickupDispatch> wrapper = new LambdaQueryWrapper<>();
        //任务类型
        if (null != dto.getTaskType()) {
            wrapper.eq(TaskPickupDispatch::getTaskType, dto.getTaskType());
        }
        //派送员
        if (StringUtils.isNotBlank(dto.getCourierId())) {
            wrapper.eq(TaskPickupDispatch::getCourierId, dto.getCourierId());
        }
        //任务分配状态
        if (null != dto.getAssignedStatus()) {
            wrapper.eq(TaskPickupDispatch::getAssignedStatus, dto.getAssignedStatus());
        }
        //任务状态
        if (null != dto.getStatus()) {
            wrapper.eq(TaskPickupDispatch::getStatus, dto.getStatus());
        }
        wrapper.orderBy(true, false, TaskPickupDispatch::getId);
        return this.page(IPage, wrapper);
    }

    /**
     * 获取 取派件任务列表
     *
     * @param dispatch 查询条件
     * @return 取派件任务列表
     */
    @Override
    public List<TaskPickupDispatch> findAll(TaskPickupDispatchDTO dispatch) {
        if (null == dispatch) {
            throw new IllegalArgumentException("参数异常!");
        }
        LambdaQueryWrapper<TaskPickupDispatch> wrapper = new LambdaQueryWrapper<>();

        if (null != dispatch.getIds() && dispatch.getIds().size() > 0) {
            wrapper.in(TaskPickupDispatch::getId, dispatch.getIds());
        }

        if (null != dispatch.getOrderIds() && dispatch.getOrderIds().size() > 0) {
            wrapper.in(TaskPickupDispatch::getOrderId, dispatch.getOrderIds());
        }
        //任务类型
        if (null != dispatch.getTaskType()) {
            wrapper.eq(TaskPickupDispatch::getTaskType, dispatch.getTaskType());
        }
        //派送员
        if (StringUtils.isNotBlank(dispatch.getCourierId())) {
            wrapper.eq(TaskPickupDispatch::getCourierId, dispatch.getCourierId());
        }
        //任务分配状态
        if (null != dispatch.getAssignedStatus()) {
            wrapper.eq(TaskPickupDispatch::getAssignedStatus, dispatch.getAssignedStatus());
        }
        //任务状态
        if (null != dispatch.getStatus()) {
            wrapper.eq(TaskPickupDispatch::getStatus, dispatch.getStatus());
        }
        wrapper.orderBy(true, false, TaskPickupDispatch::getId);

        return this.list(wrapper);
    }


    /**
     * 根据ID查询 快递单
     *
     * @param id
     * @return
     */
    @Override
    public TaskPickupDispatch findById(String id) {
        if (StringUtils.isBlank(id)) {
            throw new IllegalArgumentException("参数异常!");
        }
        return this.getById(id);
    }

    /**
     * 根据订单id和任务类型查询 快递单
     *
     * @param orderId
     * @param taskType
     * @return
     */
    @Override
    public TaskPickupDispatch findByOrderId(String orderId, Integer taskType) {
        if (StringUtils.isBlank(orderId)) {
            throw new IllegalArgumentException("参数异常!");
        }
        LambdaQueryWrapper<TaskPickupDispatch> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(TaskPickupDispatch::getOrderId, orderId);
        if (null != taskType || taskType>0) {
            wrapper.eq(TaskPickupDispatch::getTaskType, taskType);
        }
        return this.getOne(wrapper);
    }
}
